package pers.qianyu.month_202011.date_20201119;

/**
 * [重要]
 * 114. 二叉树展开为链表
 * https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/
 *
 * @author mizzle rain
 * @date 2020-11-19 20:59
 */
public class Flatten {
    public void flatten(TreeNode root) {
        recur(root);
    }

    private void recur(TreeNode node) {
        if (node == null) {
            return;
        }
        recur(node.left);
        recur(node.right);
        TreeNode right = node.right;
        node.right = node.left;
        node.left = null;
        while (node.right != null) {
            node = node.right;
        }
        node.right = right;
    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
